﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NinjaToolbox.SubComponentModule.Internal;

namespace NinjaToolbox.SubComponentModule
{
    [Serializable]
    public abstract class SubComponentModelBase : IUnionSerializable
    {
        bool mEnabled = true;
        SubComponentModelUserData mUserData;

        protected int mSerializeVersion;

        public virtual bool Enabled { get { return mEnabled; } set { mEnabled = value; } }

        public SubComponentModelUserData UserData { get { return mUserData ?? (mUserData = new SubComponentModelUserData()); } set { mUserData = value; } }


        protected virtual void EditorDeserialize(UnionSerializeReader serializeReader)
        {
            (UserData as IUnionSerializable).Deserialize(serializeReader);

            mEnabled = serializeReader.ReadBool();
            mSerializeVersion = serializeReader.ReadInt();
        }

        protected virtual void EditorSerialize(UnionSerializeWriter serializeWriter)
        {
            (UserData as IUnionSerializable).Serialize(serializeWriter);

            serializeWriter.Write(mEnabled);
            serializeWriter.Write(mSerializeVersion);
        }

        #region ---- IUnionSerializable Members ---
        void IUnionSerializable.Deserialize(UnionSerializeReader serializeReader)
        {
            EditorDeserialize(serializeReader);
        }

        void IUnionSerializable.Serialize(UnionSerializeWriter serializeWriter)
        {
            EditorSerialize(serializeWriter);
        }
        #endregion
    }
}
